<?php

namespace app\common\model;

use think\Model;
use traits\model\SoftDelete;

class Information extends Model
{
    //软删除
    use SoftDelete;

    //资讯添加
    public function information_add($data)
    {
        $validate = new \app\common\validate\Information();
        if (!$validate -> check($data)){
            return $validate -> getError();
        }
        $info = $data['img'] -> move(ROOT_PATH.'public'.DS.'uploads/information');
        if ($info){
            $data['img'] = '/uploads/information/'.$info -> getSaveName();
        } else {
            return $data -> getError();
        }
        $result = $this -> save($data);
        if ($result){
            return 1;
        } else {
            return '资讯添加失败！';
        }

    }

    //资讯编辑
    public function information_edit($data)
    {
        $validate = new \app\common\validate\Information();
        if ($data['imgs']){
            $data['img'] = $data['imgs'];
            if (!$validate -> check($data)){
                return $validate -> getError();
            }
            $info = $data['img'] -> move(ROOT_PATH.'public'.DS.'uploads/information');
            if ($info){
                $data['img'] = '/uploads/information/'.$info -> getSaveName();
            } else {
                return $data -> getError();
            }
        }
        $infor = $this -> find($data['id']);
        $infor -> title = $data['title'];
        $infor -> author = $data['author'];
        $infor -> content = $data['content'];
        $infor -> img = $data['img'];
        $result = $infor -> save();
        if ($result){
            return 1;
        } else {
            return '资讯更新失败！';
        }
    }


}
